package Huihu.src.cnitcast;

import java.util.*;
/*
模拟斗地主发牌看牌
1、买牌
2、洗牌
3、发牌
4、看牌
 */
public class SendPoker {
    public static void main(String[] args) {
        //1、买牌
        //1.1 定义一个双列集合，键：表示牌的编号  值：表示具体的牌  规则：编号越小、牌越小
        Map<Integer,String> pokers=new HashMap<>();
        //1.2定义一个单列集合，用来储存所有牌的编号
        List<Integer> list=new ArrayList<>();
        //1.3具体买牌动作
        //普通牌52张
        String [] colors={"♠","♥","♣","♦"};
        String [] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2",};
        int num=0;//表示牌的编号
        //通过循环镶套
        for (String number : numbers) {
            for (String color : colors) {
                String poker =color +number;
                //System.out.println(poker);
                //将牌的编号，具体的牌放到双列集合
                pokers.put(num,poker);
                //将牌的编号放到单列集合
                list.add(num);
                //每添加一张牌，编号要自增1
                num++;
            }
        }
        //大小王
        //添加小王
        pokers.put(num,"小王");
        list.add(num++);
        //添加大王
        pokers.put(num,"大王");
        list.add(num++);
        //打印牌就可以了
        System.out.println("所有的牌："+pokers);
        System.out.println("所有的编号："+list);

        //2、洗牌
        Collections.shuffle(list);
        System.out.println("洗好牌后，编号为："+list);

        //3、发牌
        //3.1定义4个集合，分别表示3个玩家、底牌
        List<Integer> jjy=new ArrayList<>();
        List<Integer> hcy=new ArrayList<>();
        List<Integer> tzy=new ArrayList<>();
        List<Integer> sz=new ArrayList<>();
        //3.2具体的发牌动作，将索引和3取模，决定发给谁
        for (int i = 0; i < list.size(); i++) {
            //获取编号
            Integer pokerNum=list.get(i);
            //System.out.println(pokerNum);
            if (i>=list.size()-3){
                sz.add(pokerNum);
            }
            else if (i%3==0){
                jjy.add(pokerNum);
            }
            else if (i%3==1){
                hcy.add(pokerNum);
            }
            else if (i%3==2){
                tzy.add(pokerNum);
            }
        }
        //3.3查看玩家、底牌的编号
        System.out.println("jjy:"+jjy);
        System.out.println("hcy:"+hcy);
        System.out.println("tzy:"+tzy);
        System.out.println("sz:"+sz);

        //看牌
        String  str1=printPoker(jjy,pokers);
        String  str2=printPoker(hcy,pokers);
        String  str3=printPoker(tzy,pokers);
        String  str4=printPoker(sz,pokers);
        System.out.println("jjy:"+str1);
        System.out.println("hcy:"+str2);
        System.out.println("tzy:"+str3);
        System.out.println("sz:"+str4);

    }
    public static String printPoker(List<Integer> nums,Map<Integer,String> pokers) {
        //1、对牌的编号进行升序排列
        Collections.sort(nums);
        //2、遍历牌的编号集合，获取每一个编号
        StringBuilder sb = new StringBuilder();
        for (Integer num : nums) {
            //3、根据编号去双列集合去找到对应的牌
            String poker = pokers.get(num);
            sb.append(poker + " ");
        }
        //4、将获取的牌进行拼
        //5、将最后的拼接结果返回
        String str = sb.toString();
        return str.trim();
    }
}
